/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/comm/XStatusForm.java
 Version: ###VERSION###  Date: ###DATE###
 Author: aspro
 *******************************************************************************/

package jimm;

import jimm.comm.*;
import jimm.util.*;
import java.util.*;
import javax.microedition.lcdui.*;
import DrawControls.*;


public class XStatusForm extends Form implements CommandListener 
{
    /** Creates a new instance of XtrazForm */
    private XStatusForm() 
    {
        super(ResourceBundle.getString("xtraz_msg"));
        enableChoiceGroup.append(ResourceBundle.getString("toall"), null);
        enableChoiceGroup.append(ResourceBundle.getString("toselected"), null);

        append(titleTextField);
        append(descTextField);
        append(enableChoiceGroup);
		addCommand(saveCommand);
        addCommand(backCommand);
        setCommandListener(this);
    }
    
    private TextField titleTextField      = new TextField(ResourceBundle.getString("xtraz_title"), "", 40, TextField.ANY);
    private TextField descTextField       = new TextField(ResourceBundle.getString("xtraz_desc"), "", 256, TextField.ANY);
    private ChoiceGroup enableChoiceGroup = new ChoiceGroup(ResourceBundle.getString("xtraz_enable"), Choice.EXCLUSIVE);
//    private ChoiceGroup enableBalloon = new ChoiceGroup(ResourceBundle.getString("xtraz_enable"), Choice.MULTIPLE);
    private Command saveCommand           = new Command(ResourceBundle.getString("save"), Command.SCREEN, 1);
	//#sijapp cond.if target is "MIDP2"#
    private Command backCommand           = new Command(ResourceBundle.getString("back"), Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 2);
	//#sijapp cond.else#
    private Command backCommand           = new Command(ResourceBundle.getString("back"), Command.BACK,   2);
	//#sijapp cond.end#

    private void showXtrazForm() 
    {
        int xInd = XTrazTable.xstIndex - 1;

        titleTextField.setString(Options.getXtrazTitle(xInd));
        String desc = Options.getXtrazMessage(xInd);

        descTextField.setString(desc);

//        titleTextField.setString(XStatus.getStatusAsString(MainMenu.xstIndex - 1));
//        descTextField.setString(Options.getString(Options.OPTION_XTRAZ_MESSAGE));
        enableChoiceGroup.setSelectedIndex(0, Options.getBoolean(Options.OPTION_XTRAZ_ENABLE));
        enableChoiceGroup.setSelectedIndex(1, !Options.getBoolean(Options.OPTION_XTRAZ_ENABLE));
	Jimm.display.setCurrent(this);
    }

    private static XStatusForm instance;
    
    public static void activate() {
        if (instance == null) {
            instance = new XStatusForm();
        }
        instance.showXtrazForm();
    }
    
    public void commandAction(Command command, Displayable displayable) 
    {
        if (command == saveCommand) 
        {
	    int xInd = XTrazTable.xstIndex - 1;

            Options.XTRAZ_TITLES.put(new Integer(xInd*2), titleTextField.getString());

            String desc = descTextField.getString();

            if (desc == "")
              desc = " ";  

            Options.XTRAZ_TITLES.put(new Integer(xInd*2+1), desc);


//            Options.setString(Options.OPTION_XTRAZ_TITLE, titleTextField.getString());
//            Options.setString(Options.OPTION_XTRAZ_MESSAGE, desc);

            Options.setBoolean(Options.OPTION_XTRAZ_ENABLE, enableChoiceGroup.isSelected(0));
            Options.setInt(Options.OPTION_XSTATUS, XTrazTable.xstIndex - 1);
            Options.safe_save();
            //MainMenu.rebuild();
	    Jimm.gm.refreshMainMenu();	
			if (Icq.isConnected())
			{
				ContactList.activate();
                try 
                {
                    OtherAction.setStandartUserInfo();
                } 
                catch (JimmException e) 
                {
                    JimmException.handleException(e);
                }
			}
			else
			{
				//MainMenu.activate();
				Jimm.gm.showMainMenu();
			}
        } 
        else 
        {
            //MainMenu.activate();
		Jimm.gm.showMainMenu();
        }
    }



/*

	///////////////////////////////////
	//                               // 
	//   UI for XStatus selection    //
	//                               //
	///////////////////////////////////
	
	static private Displayable lastDisplay;
	static private CommandListener selectionListener;
	static private Command cmdOk = new Command(ResourceBundle.getString("select"), Command.OK, 1); 
	//#sijapp cond.if target is "MIDP2"#
	static private Command cmdCancel = new Command(ResourceBundle.getString("cancel"), Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 2); 
	//#sijapp cond.else#
	static private Command cmdCancel = new Command(ResourceBundle.getString("cancel"), Command.BACK, 2); 
	//#sijapp cond.end#
//	static private String emotionText; 
	
	static private Selector selector;

	static public void selectXStatus(CommandListener selectionListener_, Displayable lastDisplay_)
	{
		selectionListener = selectionListener_;
		lastDisplay       = lastDisplay_;
		//selList = new TextList(null);
		selector = new Selector();
		JimmUI.setColorScheme(selector);
		
		// #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" | target is "SIEMENS2"#
		selector.setFullScreenMode(false);
		// #sijapp cond.end#
		
		selector.addCommand(cmdOk);
		selector.addCommand(cmdCancel);
//		selector.setCommandListener(this);
		
		Jimm.display.setCurrent(selector);
	}
	
	public void commandAction(Command c, Displayable d)
	{
		if (c == cmdOk) select(1);
		else if (c == cmdCancel)
		{
			Jimm.display.setCurrent(lastDisplay);
			selector = null;
		};
	}
	
	public boolean onKeyPress(VirtualList sender, int keyCode,int type) {}
	public void onCursorMove(VirtualList sender) {}
	public void onItemSelected(VirtualList sender) 
	{
		select(1);
	}

	static private void select(int n)
	{
		Jimm.display.setCurrent(lastDisplay);        
		selector = null;
		System.gc();
		selectionListener.commandAction(cmdOk, selector);
	}
	
	static public String getSelectedEmotion()
	{
	}
	
	static public boolean isMyOkCommand(Command command)
	{
		return (command == cmdOk);
	}
	
	
	
	/////////////////////////
	//                     //
	//    class Selector   //
	//                     //
	/////////////////////////
		
	static class Selector extends VirtualList implements VirtualListCommands
	{
		static private int cols, rows, itemHeight, curCol;
		static private Selector _this;
	
		Selector()
		{
			super(null);
			_this = this;
			setVLCommands(this);
			
			int drawWidth = getWidth()-scrollerWidth-2;
			
			setCursorMode(SEL_NONE);
			
			int imgHeight = images.getHeight();
			
			itemHeight = imgHeight+2;
			
			cols = drawWidth/itemHeight;
			rows = (selEmotionsIndexes.length+cols-1)/cols;
			curCol = 0;
			
			showCurrSmileName();
		}
		
		//#sijapp cond.if target is "MIDP2"#
		protected boolean pointerPressedOnUtem(int index, int x, int y)
		{
			int lastCol = curCol; 
			curCol = x/itemHeight;
			if (curCol < 0) curCol = 0;
			if (curCol >= cols) curCol = cols-1;
			if (lastCol != curCol)
			{
				showCurrSmileName();
				invalidate();
			}
				
			return false;
		}
		// #sijapp cond.end#
		
		protected void drawItemData
		(
			Graphics g, 
		    boolean isSelected, 
		    int index, 
		    int x1, int y1, int x2, int y2,
		    int fontHeight
		)
		{
			int xa, xb;
			int startIdx = cols*index;
			int imagesCount = images.size();
			xa = x1;
			for (int i = 0; i < cols; i++, startIdx++)
			{
				if (startIdx >= selEmotionsIndexes.length) break;
				int smileIdx = selEmotionsIndexes[startIdx]; 
				
				xb = xa+itemHeight;
				
				if (smileIdx < imagesCount)
					g.drawImage(images.elementAt(smileIdx), xa+1, y1+1, Graphics.TOP|Graphics.LEFT);
				
				if (isSelected && (i == curCol))
				{
					g.setColor(this.getTextColor());
					g.setStrokeStyle(Graphics.DOTTED);
					g.drawRect(xa, y1, itemHeight-1, y2-y1-1);
				}
				xa = xb;
			}
		}
		
		static private void showCurrSmileName()
		{
			int selIdx = _this.getCurrIndex()*cols+curCol;
			if (selIdx >= selEmotionsSmileNames.length) return;
			emotionText = selEmotionsWord[selIdx];			
			_this.setCaption(selEmotionsSmileNames[selIdx]);
		}
		
		public int getItemHeight(int itemIndex)
		{
			return itemHeight;
		}
		
		protected int getSize()
		{
			return rows;
		}
		
		protected void get(int index, ListItem item)
		{
			
		}
		
		public void onKeyPress(VirtualList sender, int keyCode, int type) 
		{
			if (type == VirtualList.KEY_PRESSED)
			{
			int lastCol = curCol;
			int curRow = getCurrIndex();
			int rowCount = getSize();


			
			if (keyCode == 52)
			{
				select(2);
				return;
			}
			if (keyCode == 54)
			{
				select(3);
				return;
			}


			switch (getGameAction(keyCode))
			{
			case LEFT:
				if (curCol != 0) curCol--;
				else if (curRow != 0)
				{
					curCol = cols-1;
					curRow--;
				}
				break;
				
			case RIGHT:	
				if (curCol < (cols-1)) curCol++;
				else if (curRow <= rowCount)
				{
					curCol = 0;
					curRow++;
				}
				break;


			case KEY_NUM6:
			{
				select(3);		
				return;
			}
			}
			
			setCurrentItem(curRow);
			
			int index = curCol+getCurrIndex()*cols;
			if (index >= selEmotionsIndexes.length) curCol = (selEmotionsIndexes.length-1)%cols; 
			
			if (lastCol != curCol)
			{
				invalidate();
				showCurrSmileName();
			}
			
		}
		}
		
		public void onCursorMove(VirtualList sender) 
		{
			showCurrSmileName();
		}
		
		public void onItemSelected(VirtualList sender) 
		{
			select(1);
		}
	}


*/

}       